<?php
/*
 * 用用于查找请求中包含的cmd参数。假设参数被找到，并与command目录中的类文件相匹配，而该文件也正好包含cmd类，则该方法创建并返回相应类的对象实例。
 * 如果这些条件未满足，getCommand()方法使用默认的Command对象。
 *
*/
class WooCommandResolver{
	private static $base_cmd;
	private static $default_cmd;

	function __construct(){
		if(! self::$base_cmd){
			self::$base_cmd = new ReflectionClass("WooCommandCommand");
			self::$default_cmd = new WooCommandDefaultCommand();
		}
	}


	function getCommand(WooControllerRequest $request){
		//var_dump($request);
		$cmd = $request->getProperty('cmd');
		$sep = DIRECTORY_SEPARATOR;
		if(! $cmd){
			return self::$default_cmd;
		}

		$cmd = str_replace(array('.',$sep),"",$cmd);
		$cmd = ucfirst($cmd);
		$filepath = "WooCommand{$cmd}.php";
		$classname = "WooCommand$cmd";
		//var_dump($cmd,$filepath,$classname);
		//echo $filepath;
		if(file_exists($filepath)){
			//echo $filepath;
			//@require_once("$filepath");
			//var_dump(self::$base_cmd);
			if(class_exists($classname)){
				//echo 222;
				$cmd_class = new ReflectionClass($classname);
				//if($cmd_class->isSubclassOf(self::$base_cmd)){
				if($cmd_class){
					//echo 333;die();
					//var_dump($cmd_class);
					return $cmd_class->newInstance();
				}else{
					//echo 444;
					$request->addFeedback("command '$cmd' is not a Command");
				}
			}
		}
		$request->addFeedback("command '$cmd'  not found");
		return clone self::$default_cmd;
	}
	
}